package com.jt.safe.extension

import android.content.Context
import android.content.SharedPreferences
import com.jt.safe.BuildConfig
import com.jt.safe.config.BaseApplication

fun Any.SP(name: String = BuildConfig.APPLICATION_ID) =
    BaseApplication.INSTANCE.getSharedPreferences(name, Context.MODE_PRIVATE)


/**
 * 批处理
 */
fun SharedPreferences.edit(action: SharedPreferences.Editor.() -> Unit) {
    edit().apply { action() }.apply()
}

/**
 * 传入基本类型
 */
fun SharedPreferences.put(key: String, value: Any) {
    edit {
        when (value) {
            is String -> putString(key, value)
            is Int -> putInt(key, value)
            is Boolean -> putBoolean(key, value)
            is Float -> putFloat(key, value)
            is Long -> putLong(key, value)
        }
    }
}

fun SharedPreferences.clear() {
    edit { clear() }
}